const App = getApp()
Page({
  data: {
    inputVal: '',
    hotWords: [],
    list: []
  },
  onLoad() {
    wx.showNavigationBarLoading()
    this.getList(this.data.inputVal, () => {
      wx.hideNavigationBarLoading()
    })
    this.getCarHot()
  },
  clearInput() { // 清空搜索框
    this.setData({
      inputVal: ''
    })
    this.search()
  },
  inputTyping(e) {
    this.setData({
      inputVal: e.detail.value
    })
    this.search()
  },
  search() { // 搜索
    this.getList(this.data.inputVal)
  },
  toSearch(e) { // 热门车型点击
    let {
      hotword
    } = e.currentTarget.dataset
    this.setData({
      inputVal: hotword
    })
    this.getList(hotword)
  },
  getCarHot() { // 获取热门车型
    App.HttpService.getCarHot().then(res => {
      let data = res.data.data
      this.setData({
        hotWords: data
      })
    })
  },
  getList(inputVal, cb) { // 获取车辆品牌
    App.HttpService.carBrands({
      carBrandName: inputVal
    }).then(res => {
      cb && cb()
      let data = res.data
      if (data.code == 200) {
        this.setData({
          list: data.data
        })
      }
    })
  },
  toNext(e) {
    console.log(e)
    var type = e.currentTarget.dataset.ty

    if (type == 1) {
      let secondMenu = e.currentTarget.dataset
      wx.setStorageSync('secondMenu', secondMenu)
      wx.navigateTo({
        url: '/pages/car/thirdMenu/index'
      })
    } else {   
      let firstMenu = e.currentTarget.dataset
      wx.setStorageSync('firstMenu', firstMenu)
      wx.navigateTo({
        url: '/pages/car/secondMenu/index'
      })

    }


  }
})